package org.testis.serialportmodule.services;

import org.testis.data.TestStepDataObject;
import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.serialportmodule.data.SerialPortModuleDataObject;
import org.testis.services.AtofModuleService;
import org.testis.services.AtofModuleTask;
import org.testis.testdriver.AtofEnumaration.AtofSerialPortModuleAction;

public class SerialPortModuleTask extends AtofModuleTask {

    public SerialPortModuleTask(TestStepDataObject dataObj,
            AtofModuleService service) {
        super(dataObj,
                service);
    }

    @Override
    public void run() {
        Exception currentException = null;
        try {
            boolean response = m_service.runTest();
            m_result.setResult(response);
            if (response) {
                m_result.setResultType(TestStepResultType.PASS);
                SerialPortModuleDataObject dataObj = (SerialPortModuleDataObject) m_service.getDataObj();
                if (dataObj.getActionType().equals(AtofSerialPortModuleAction.StartConnection.name())) {
                    Thread.sleep(Long.MAX_VALUE);
                }
            } else {
                m_result.setResultType(TestStepResultType.ERROR);
            }
        } catch (TestStepException ex) {
            m_result.setResultType(ex.getResultType());
            currentException = ex;
        } catch (RuntimeException ex) {
            m_result.setResultType(TestStepResultType.FAILURE);
            currentException = ex;
        } catch (InterruptedException ex) {
            m_result.setResultType(TestStepResultType.PASS);
            currentException = ex;
        } catch (Exception ex) {
            m_result.setResultType(TestStepResultType.FAILURE);
            currentException = ex;
        }
        if (currentException != null) {
            try {
                LogSenderService.putMessageOnQueue("AtofModuleTask",
                        AtofLogLevel.ERROR,
                        currentException.getMessage(),
                        m_service.getDataObj().
                        getMessageId());
            } catch (Exception ex) {
            }
        }
    }

    @Override
    public void afterExecute() {
        SerialPortModuleProviderService service =
                (SerialPortModuleProviderService) m_service;
        SerialPortModuleDataObject dataObj =
                (SerialPortModuleDataObject) m_service.getDataObj();
        try {
            if (dataObj.getActionType().equals(AtofSerialPortModuleAction.StartConnection.name())) {
                if (service.m_serialConnection != null) {
                    service.m_serialConnection.endConnection();
                    LogSenderService.putMessageOnQueue(
                            "SerialPortModuleStartConnectionAction",
                            AtofLogLevel.INFO,
                            "End serial connection on port  COM" + dataObj.getComPortNo(),
                            dataObj.getMessageId());
                }
            }
        } catch (Exception ex) {
        }
        super.afterExecute();
    }
}
